package com.softeem.god.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.god.comm.R;
import com.softeem.god.entity.Music;
import com.softeem.god.entity.MusicList;
import com.softeem.god.entity.MusicListDetails;
import com.softeem.god.service.MusicListDetailsService;
import com.softeem.god.service.MusicListService;
import com.softeem.god.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import java.util.Date;
import java.util.List;

@Tag(name = "歌单管理")
@RestController
@RequestMapping("/musicList")
public class MusicListController {

    @Autowired
    private MusicController musicController;
    @Autowired
    private MusicListService musicListService;
    @Autowired
    private MusicListDetailsService musicListDetailsService;


    @Operation(summary = "歌单列表",description = "分页查询歌单列表")
    @Parameters(value = {@Parameter(name = "page",description = "当前页码"),
            @Parameter(name = "limit",description = "每页显示数量")})

    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer limit){
        //创建分页对象 limint ？，？
        IPage<MusicList> ipage = new Page<>(page,limit);
        //执行查询(分页)
        List<MusicList> list = musicListService.list(ipage);
        //查询总记录数
        long count = musicListService.count();
        //返回结果
        return R.success().setData(list).setCount(count);
    }
    
    @Operation(summary = "添加歌单", description = "添加新的歌单")
    @PostMapping("/add")
    public R add(@RequestBody MusicList musicList) {
        // 设置创建时间
        musicList.setCreateTime(new Date());
        // 设置默认状态为上架(1)
        musicList.setStatus("1");
        // 设置默认用户ID为1
        if (musicList.getUid() == null) {
            musicList.setUid(1);
        }
        boolean result = musicListService.save(musicList);
        return result ? R.success().setMsg("添加成功") : R.error().setMsg("添加失败");
    }


    @Transactional
    @Operation(summary = "删除歌单", description = "根据ID删除歌单")
    @Parameters(value = {@Parameter(name = "id", description = "歌单ID")})
    @DeleteMapping("/delete")
    public R delete(@RequestParam Integer id) {
        try {
            // 删除歌单详情表中与该歌单关联的所有记录
            QueryWrapper<MusicListDetails> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("list_id", id);
            
            // 删除歌单详情记录（无论是否有匹配记录都继续执行）
            musicListDetailsService.remove(queryWrapper);
            
            // 删除歌单本身
            boolean result = musicListService.removeById(id);
            if (result) {
                return R.success().setMsg("删除成功");
            } else {
                return R.error().setMsg("删除失败，未找到指定歌单");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().setMsg("删除失败: " + e.getMessage());
        }
    }
    
    @Operation(summary = "更新歌单", description = "更新歌单信息")
    @PutMapping("/update")
    public R update(@RequestBody MusicList musicList) {
        boolean result = musicListService.updateById(musicList);
        return result ? R.success().setMsg("更新成功") : R.error().setMsg("更新失败");
    }


}
